{
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
}

unit RegexpPatternOptions;

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Scene, superobject, Dispatcher, VisualUtils;

type
  TRegexpPatternOptionsForm = class(TForm)
    ApplicationProperties: TApplicationProperties;
    btnCancel: TButton;
    btnOk: TButton;
    cbPreprocessSpaces: TCheckBox;
    cbIgnoreCase: TCheckBox;
    gbxMain: TGroupBox;
    pnlButtons: TPanel;
    rbRemove: TRadioButton;
    rbNormalize: TRadioButton;
    procedure ApplicationPropertiesIdle(Sender: TObject; var Done: Boolean);
    procedure btnOkClick(Sender: TObject);
  private
    FId: TShapeId;
    FOptions: ISuperObject;
    FDispatcher: TDispatcher;
    procedure Prepare;
    { private declarations }
  public
    class procedure OpenModal(const Id: TShapeId; const Options: ISuperObject; Dispatcher: TDispatcher);
    { public declarations }
  end; 

implementation

{$R *.lfm}

const
  KEEP = 'KEEP';
  REMOVE = 'REMOVE';
  NORMALIZE = 'NORMALIZE';

{ TRegexpPatternOptionsForm }

procedure TRegexpPatternOptionsForm.ApplicationPropertiesIdle(Sender: TObject;
  var Done: Boolean);
begin
  rbRemove.Enabled := cbPreprocessSpaces.Checked;
  rbNormalize.Enabled := cbPreprocessSpaces.Checked;
end;

procedure TRegexpPatternOptionsForm.btnOkClick(Sender: TObject);
var
  Input: ISuperObject;
  m: String;
begin
  Input := SO;
  if cbPreprocessSpaces.Checked then
  begin
    if rbNormalize.Checked then
      m := NORMALIZE
    else
      m := REMOVE
  end
  else
    m := KEEP;
  Input.S['spacePreprocessMode'] := m;
  Input.B['caseSensitive'] := not cbIgnoreCase.Checked;

  FDispatcher.PerformAction(FId, 'setOptions', Input);
  ModalResult := mrOk;
end;

procedure TRegexpPatternOptionsForm.Prepare;
var
  m: String;
begin
  m := FOptions.S['spacePreprocessMode'];
  cbPreprocessSpaces.Checked := m <> KEEP;
  if m = NORMALIZE then
    rbNormalize.Checked := TRUE
  else if m = REMOVE then
    rbRemove.Checked := TRUE;
  cbIgnoreCase.Checked := not FOptions.B['caseSensitive'];
end;

class procedure TRegexpPatternOptionsForm.OpenModal(const Id: TShapeId; const Options: ISuperObject; Dispatcher: TDispatcher);
var
  Form: TRegexpPatternOptionsForm;
begin
  Form := TRegexpPatternOptionsForm.Create(nil);
  try
    RestoreFormPosition(Form);

    Form.FId := Id;
    Form.FOptions := Options;
    Form.FDispatcher := Dispatcher;
    Form.Prepare;

    ShowModalForm(Form);
  finally
    Form.Free;
  end;
end;

end.

